package org.lifeaddict.sybill;
import java.io.BufferedReader;
import java.util.ArrayList;


public class Parser {
	
	public static String parseName(ArrayList<String> in) {
		String out = "";
		for(String i : in) {
			if(i.contains("Zur Visitenkarte...")) {
				i = i.split("title=\"Zur Visitenkarte...\">")[1];
				out = i.split("</A></TD>")[0];
				break;
			}
		}
		return out;
		
	}
	
	public static String parsePersonId(ArrayList<String> in) {
		String out = "";
		for(String i : in) {
			if(i.contains("visitenkarte.show")) {
				i = i.split("pPersonenId=")[1];
				out = i.split("&pPersonenGruppe=")[0];
				break;
			}
		}
		return out;
	}
	
	public static String parsePersonGrp(ArrayList<String> in) {
		String out = "";
		for(String i : in) {
			if(i.contains("visitenkarte.show")) {
				i = i.split("pPersonenGruppe=")[1];
				out = i.split("\" TARGET=\"detail\"")[0];
				break;
			}
		}
		return out;
	}
	
	public static String parsePersonNr(ArrayList<String> in) {
		String out = "";
		for(String i : in) {
			if(i.contains("visitenkarte.student_vcard")) {
				i = i.split("cst_person_nr=")[1];
				out = i.split("\" class=\"nav\"")[0];
				break;
			}
		}
		return out;
	}
	
	public static ArrayList<String> parseLvs(BufferedReader reader) {
		ArrayList<String> in = readToList(reader, "clvnr=");
		ArrayList<String> out = new ArrayList<String>();
		for(String i : in) {
			if(i.contains("clvnr=")) {
				i = i.split("clvnr=")[1];
				i = i.split("&")[0];
				i = i.split("\"")[0];
				if (!out.contains(i))
					out.add(i);
			}
		}
		return out;
	}
	
	public static String parseLvName(ArrayList<String> in) {
		String out = "";
		for(String i : in) {
			if (i.contains("<TD NOWRAP class=\"pageOwner\">")) {
				out = i;
				break;
			}
		}
		out = out.split("<span class=\"s\">")[1];
		out = out.split(" ",2)[1];
		out = out.split(" \\(")[0];
		return out;
	}
	
	public static void parseLvEvents(ArrayList<String> eventData, LvEvent lvevent) {
		String date, time1, time2, location;
		LvEventDate evDate;
		for (String ev : eventData) {
			if (ev.contains("title=\"Detailansicht\"")) {
				ev = ev.split("title=\"Detailansicht\">")[1];
				date = ev.split("</a></td>")[0];
				ev = ev.split("\" L\">", 2)[1];
				time1 = ev.split("</td>",2)[0];
				ev = ev.split("\" L\">", 2)[1];
				time2 = ev.split("</td>",2)[0];
				if (ev.contains("href")) {
					ev = ev.split("\\)\">", 2)[1];
					location = ev.split(" \\(\\d")[0];
				} else {
					ev = ev.split("\" L\">", 2)[1];
					location = ev.split("</td>",2)[0];
				}
				evDate = new LvEventDate(location, date, time1, time2);
				lvevent.addDate(evDate);
			}
		}	
	}

	public static ArrayList<String> readToListAll(BufferedReader reader) {
		ArrayList<String> out = new ArrayList<String>();
		String inputLine = "";
		try {
			while ((inputLine = reader.readLine()) != null)
				out.add(0, inputLine);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}
	
	public static ArrayList<String> readToList(BufferedReader reader, String stop) {
		ArrayList<String> out = new ArrayList<String>();
		String inputLine = "";
		try {
			while ((inputLine = reader.readLine()) != null)
				if(inputLine.contains(stop)) {
					out.add(inputLine);
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}
	
	public static ArrayList<String> readToListSingle(BufferedReader reader, String stop) {
		ArrayList<String> out = new ArrayList<String>();
		String inputLine = "";
		try {
			while ((inputLine = reader.readLine()) != null)
				if(inputLine.contains(stop)) {
					out.add(inputLine);
					break;
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}
}
